package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Student;
import com.sptpc.demo1.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stu")
public class StudentsControoller {
    @Autowired
    public IStudentService studentService;


    // TODO:获取到数据库的所有学生对象
    @GetMapping("/all")
    public List<Student> gatAllStudent() {
        log.info("查询所有对象数据");
        List<Student> lists = studentService.getAllStudents();
        return lists;
    }

    //TODO:通过学生ID查询学生信息
    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Integer id) {
        log.info("查询学生信息ID={}", id);
        //判断是否有该学生对象
        if (studentService.getStudentByID(id) != null) {
            return studentService.getStudentByID(id);
        } else {
            //返回空数据
            return (Student.builder()
                    .id(404)
                    .name("未查询到学生信息")
                    .adminssionDate(LocalDate.parse("2000-01-01"))
                    .major("空")
                    .build());
        }
    }

    // TODO:添加学生信息
    @PostMapping("/add")
    public Student addNewStudent(@RequestBody Student student) {
        log.info("添加学生对象成功{}", student);
        int aID = student.getId();
        //判断是否有该id学生
        if (studentService.getStudentByID(aID) != null) {
            return (Student.builder()
                    .id(405)
                    .name("该id数据已存在")
                    .adminssionDate(LocalDate.parse("2000-01-01"))
                    .major("空")
                    .build());
        } else {
            studentService.addStudent(student);
            return student;
        }

    }

    // TODO:删除学生信息
    @DeleteMapping("/del/{id}")
    public String delStudent(@PathVariable Integer id) {
        log.info("删除学生ID={}", id);
        Student fDelstudent = studentService.getStudentByID(id);
        if (fDelstudent != null) {
            studentService.delStudentByID(id);
            //显示删除学生的信息
            return "删除学生信息成功 \n 学生姓名:" + fDelstudent.name;
        } else {
            return "删除学生信息失败 没有该ID学生";
        }
    }

    //TODO:更新学生信息
    @PutMapping("/updata")
    public Student setStudent(@RequestBody Student request) {
        log.info("修改学生数据{}", request);
        //被修改学生的ID
        int f_setStudentid = request.getId();
        //被修改学生信息
        Student f_setStudnetdate = studentService.getStudentByID(f_setStudentid);
        //是否有该学生信息
        if (f_setStudnetdate != null) {
            //修改数据
            studentService.setStudentDate(request);
            return studentService.getStudentByID(f_setStudentid);
        } else {
            //返回空数据
            return (Student.builder()
                    .id(404)
                    .name("未查询到学生信息")
                    .adminssionDate(LocalDate.parse("2000-01-01"))
                    .major("空")
                    .build());
        }
    }


    /**
     * 批量上传学生
     */
    @PostMapping(value = "/batch", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> batchAddStudents(MultipartFile file) {
        List<Student> results = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] arge = line.split(",");
                results.add(Student.builder().name(arge[0])
                        .adminssionDate(LocalDate.parse(arge[1]))
                        .major(arge[2])
                        .build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }
}

